<?php

class AccessModel extends RelationModel {

	
	
	

    public function nodeList() {
        import("@.ORG.Category");
        $cat = new Category('Node', array('id', 'pid', 'title', 'fullname'));
        $temp = $cat->getList();               //获取分类结构
        $level = array("1" => "项目（GROUP_NAME）", "2" => "模块(MODEL_NAME)", "3" => "操作（ACTION_NAME）");
        foreach ($temp as $k => $v) {
            $temp[$k]['statusTxt'] = $v['status'] == 1 ? "启用" : "禁用";
            $temp[$k]['chStatusTxt'] = $v['status'] == 0 ? "启用" : "禁用";
            $temp[$k]['level'] = $level[$v['level']];
            $list[$v['id']] = $temp[$k];
        }
        unset($temp);
        return $list;
    }

    public function roleList() {
        $M = M("Role");
        $list = $M->select();
        foreach ($list as $k => $v) {
            $list[$k]['statusTxt'] = $v['status'] == 1 ? "启用" : "禁用";
            $list[$k]['chStatusTxt'] = $v['status'] == 0 ? "启用" : "禁用";
        }
        return $list;
    }

    public function opStatus($op = 'Node') {
        $M = M("$op");
        $datas['id'] = (int) $_GET["id"];
        $datas['status'] = $_GET["status"] == 1 ? 0 : 1;
        if ($M->save($datas)) {
            return array('status' => 1, 'info' => "处理成功", 'data' => array("status" => $datas['status'], "txt" => $datas['status'] == 1 ? "禁用" : "启动"));
        } else {
            return array('status' => 0, 'info' => "处理失败");
        }
    }

    public function editNode() {
        $M = M("Node");
        return $M->save($_POST) ? array('status' => 1, info => '更新节点信息成功', 'url' => U('Access/nodeList')) : array('status' => 0, info => '更新节点信息失败');
    }

    public function addNode() {
        $M = M("Node");
        return $M->add($_POST) ? array('status' => 1, info => $_POST['id'] . '添加节点信息成功', 'url' => U('Access/nodeList')) : array('status' => 0, info => '添加节点信息失败');
    }

    /**
      +----------------------------------------------------------
     * 管理员列表
      +----------------------------------------------------------
     */
    public function adminList() {
        $list = M("User")->select();
        foreach ($list as $k => $v) {
            $list[$k]['statusTxt'] = $v['status'] == 1 ? "启用" : "禁用";
        }
        return $list;
    }

    /**
      +----------------------------------------------------------
     * 添加管理员
      +----------------------------------------------------------
     */
    public function addAdmin() {
        if (!is_email($_POST['email'])) {
            return array('status' => 0, 'info' => "邮件地址错误");
        }
        $datas = array();
        $M = M("User");
        $datas['user_email'] = trim($_POST['email']);
        if ($M->where("`user_email`='" . $datas['user_email'] . "'")->count() > 0) {
            return array('status' => 0, 'info' => "已经存在该账号");
        }
        $datas['user_pass'] = encrypt(trim($_POST['password']));
        $datas['user_registered'] = date("Y-m-d H:m:s");
        $datas['user_intro'] = trim($_POST['user_intro']);
        $datas['user_status'] = (int)($_POST['user_status']);
        if(!isset($_POST['display_name'])) {
            $datas['user_login'] = $_POST['email'];
            $datas['display_name'] = $_POST['email'];
        }
        if ($M->add($datas)) {
            M("role_users")->add(array('user_id' => $M->getLastInsID(), 'role_id' => (int) $_POST['role_id']));
            if (C("SYSTEM_EMAIL")) {
                $body = "你的账号已开通，登录地址：" . C('WEB_ROOT') . U("Public/index") . "<br/>登录账号是：" . $datas["user_email"] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;登录密码是：" . $_POST['password'];
                $info = send_mail($datas["user_email"], "", "开通账号", $body) ? "添加新账号成功并已发送账号开通通知邮件" : "添加新账号成功但发送账号开通通知邮件失败";
            } else {
                $info = "账号已开通，请通知相关人员";
            }
            return array('status' => 1, 'info' => $info, 'url' => U("Access/index"));
        } else {
            return array('status' => 0, 'info' => "添加新账号失败，请重试");
        }
    }

    /**
      +----------------------------------------------------------
     * 添加管理员
      +----------------------------------------------------------
     */
    public function editAdmin() {
    	
    	Log::write(array_to_str($_POST));
        $User = D("User");
        if (!empty($_POST['password'])) {
            $data['user_pass'] = encrypt(trim($_POST['password']));
        } else {
            unset($_POST['password']);
        }
        
       //print_array($_POST);
        
        $user_id = $_POST['user_id0'];
        $role_id = (int) $_POST['role_id'];
       // $data['user_id'] = (int) $_POST['user_id0'];
        $data['user_login'] =  $_POST['user_login'];
        
        $data['user_email'] = $_POST['user_email'];
        $data['user_url'] = $_POST['user_url'];
       
        $data['user_nicename'] = $_POST['user_nicename'];
        $data['user_status'] = $_POST['user_status'];
        $data['user_intro'] = $_POST['user_intro'];
     
        
        $roleStatus = M("Role_users")->where("`user_id`=" . $user_id)->save(array('role_id' => $role_id));
        //print_array($data);
       // echo M("Role_users")->getLastSql();
        
        
        if ($User->where(array('user_id' => $user_id))->save($data)) {
        	 Log::write($User->getLastSql()) ; 
            return $roleStatus == TRUE ? array('status' => 1, 'info' => "成功更新", 'url' => U("Access/index")) : array('status' => 1, 'info' => "成功更新，但更改用户所属组未更新", 'url' => U("Access/index"));
        } else {
        		 Log::write($User->getLastSql()) ; 
            return $roleStatus == TRUE ? array('status' => 1, 'info' => "用户未更新，但更改用户所属组更新成功", 'url' => U("Access/index")) : array('status' => 0, 'info' => "所属用户组未更新，请重试");
        }
    }
    
    
    public function delAdmin($id=-1) {
    	

    }

    /**
      +----------------------------------------------------------
     * 添加管理员
      +----------------------------------------------------------
     */
    public function editRole() {
        $M = M("Role");
        if ($M->save($_POST)) {
            return array('status' => 1, 'info' => "成功更新", 'url' => U("Access/roleList"));
        } else {
            return array('status' => 0, 'info' => "更新失败，请重试");
        }
    }

    /**
      +----------------------------------------------------------
     * 添加管理员
      +----------------------------------------------------------
     */
    public function addRole() {
        $M = M("Role");
        if ($M->add($_POST)) {
            return array('status' => 1, 'info' => "成功添加", 'url' => U("Access/roleList"));
        } else {
            return array('status' => 0, 'info' => "添加失败，请重试");
        }
    }

    public function changeRole() {
        $M = M("Access");
        $role_id = (int) $_POST['id'];
        $M->where("role_id=" . $role_id)->delete();
        $data = $_POST['data'];
        if (count($data) == 0) {
            return array('status' => 1, 'info' => "清除所有权限成功", 'url' => U("Access/roleList"));
        }
        $datas = array();
        foreach ($data as $k => $v) {
            $tem = explode(":", $v);
            $datas[$k]['role_id'] = $role_id;
            $datas[$k]['node_id'] = $tem[0];
            $datas[$k]['level'] = $tem[1];
            $datas[$k]['pid'] = $tem[2];
        }
        if ($M->addAll($datas)) {
            return array('status' => 1, 'info' => "设置成功", 'url' => U("Access/roleList"));
        } else {
            return array('status' => 0, 'info' => "设置失败，请重试");
        }
    }

}

?>
